package com.atguigu.gmall.cart.exection.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Arrays;

@Slf4j
@Component
public class CartExceptionHandler implements AsyncUncaughtExceptionHandler {

    @Autowired
    StringRedisTemplate redisTemplate;

    private static final String EXCETION_KEY= "exction:cart";



    @Override
    public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
        log.error("购物车的异步任务出现异常。异常信息：{}，异步方法：{}，参数：{}",throwable.getMessage(),method.getName(), Arrays.asList(objects));
        BoundSetOperations<String, String> boundSetOps = redisTemplate.boundSetOps(EXCETION_KEY);
        if (objects!=null&&objects.length>0){
            boundSetOps.add(objects[0].toString());
        }
    }
}
